var common = require('../../../utils/common.js')
var app = getApp()
Page({
    data: {
        __PUBLIC__:app.__PUBLIC__ ,
        displayFile:false,        
    },
    onLoad: function (options) {
        common.bestCurl('GET','/cars/details',res=>{ 
            if(res.statusCode==200){
                if(res.data.code!=0){
                    if(res.data.car_info_data){
                        var time = new Date().getTime();
                        var currentTime = res.data.car_info_data.machine_report_time+3600000;
                        if(time > currentTime){
                            this.setData({network_status:'离线'});
                        }else{
                            this.setData({network_status:'在线'});
                        }
                        res.data.car_info_data.machine_report_time = this.checkTime(res.data.car_info_data.machine_report_time);
                        res.data.car_info_data.machine_platform_receive_time = this.checkTime(res.data.car_info_data.machine_platform_receive_time);
                    }else if(res.data.bolutu_data){
                        res.data.bolutu_data.gps_time = this.checkTime(res.data.bolutu_data.gps_time);
                        res.data.bolutu_data.accept_time = this.checkTime(res.data.bolutu_data.accept_time);
                        var time = new Date().getTime();
                        var currentTime = res.data.bolutu_data.time + 3600000;
                        if(time > currentTime){
                            this.setData({network_status:'离线'});
                        }else{
                            this.setData({network_status:'在线'});
                        } 
                        res.data.bolutu_data.time = this.checkTime(res.data.bolutu_data.time);
                    }
                    if(res.data.car_file.length!=0){
                        this.setData({details:res.data,displayFile:true})   
                    }else{
                        this.setData({details:res.data,displayFile:false}) 
                    }
                }
            }
        },{'id':options.id},'../../')
    },
    showFile:function (event) {
        var url = event.currentTarget.dataset.url;
        console.log(url);
        var fileUrl = app.__UPLOADS__+'/'+url;
        var extStart = url.lastIndexOf(".");
        var ext = url.substring(extStart, url.length).toUpperCase();
        var pic = [
            '.JPG',
            '.PNG',
            '.JPEG',
            '.BMP',
            '.GIF',
            '.WEBP'
        ];
        var fileType = [
            '.DOC',
            '.XLS',
            '.PPT',
            '.PDF',
            '.DOCX',
            '.XLSX',
            '.PPTX'
        ];
        if(pic.indexOf(ext)!=-1){
            wx.previewImage({
                current: '', 
                urls: [
                    fileUrl
                ] 
            })
        }else if(fileType.indexOf(ext)!=-1){
            wx.showLoading({
              title: '加载中',
              mask:true
            });
            wx.downloadFile({
                url: fileUrl, 
                success: function(res) {
                    wx.hideLoading();
                    if (res.statusCode === 200) {
                        wx.openDocument({
                            filePath: res.tempFilePath,
                            success: function (res) {
                                console.log('打开文档成功')
                            }
                        })
                    }
                }
            });
        }else{
            wx.showModal({
                content: '非法文件,禁止预览',
                showCancel: false,
            });        
        }       
    },
    binding:function(event){
        id = event.currentTarget.id;
        wx.showModal({
            title: '确认要绑定吗?',
            cancelText: "取消",
            confirmText: "确定",
            success: function (res) {
                if (res.confirm) {
                    common.bestCurl('GET','/cars/bind_car',res=>{      
                        if(res.statusCode==200){
                            if(res.data.result==0){
                                wx.showToast({
                                    title: '绑定成功',
                                    icon: '',
                                    duration: 2000
                                });
                            }else if(res.data.result==1){
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: "none",
                                    duration: 2000
                                });
                            }
                        }
                    },{'id':id},'../../')
                }else{
                    console.log('用户点击辅助操作')
                }
            }
        });
    },
    unbind:function(event){
        id = event.currentTarget.id;
        wx.showModal({
            title: '确认要解绑吗?',
            cancelText: "取消",
            confirmText: "确定",
            success: function (res) {
                if (res.confirm) {
                    common.bestCurl('GET','/cars/unbind_car',res=>{      
                        if(res.statusCode==200){
                            if(res.data.result==0){
                                wx.showToast({
                                    title: '解绑成功',
                                    icon: '',
                                    duration: 2000
                                });
                            }else if(res.data.result==1){
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: "none",
                                    duration: 2000
                                });
                            }
                        }
                    },{'id':id},'../../')
                }else{
                    console.log('用户点击辅助操作')
                }
            }
        });
    },
    edit:function(event){
        id = event.currentTarget.id;
        wx.navigateTo({
            url: '../edit/edit?id='+id
        })
    },
    delete:function(event){
        id = event.currentTarget.id;
        wx.showModal({
            title: '确认要删除吗?',
            cancelText: "取消",
            confirmText: "确定",
            success: function (res) {
                if (res.confirm) {
                    common.bestCurl('GET','/cars/delete',res=>{
                        if(res.statusCode == 200){
                            if(res.data.code == 1){
                                wx.showToast({
                                    title: '删除成功',
                                    icon: '',
                                    duration: 2000
                                });
                                setTimeout(res=>{
                                    wx.redirectTo({
                                        url: '../list/list'
                                    })

                                },2000)
                            }else if(res.data.code == 3){
                                wx.showToast({
                                    title: '删除失败,请解除绑定',
                                    icon: '',
                                    duration: 3000
                                });
                            }else{
                                console.log(res.data.msg)
                            }
                        }
                    },{'id':id},'../../')
                }else{
                    console.log('用户点击辅助操作')
                }
            }
        });
    },
    remoteControl:function(event){
        var no = event.currentTarget.dataset.no;
        var id = event.currentTarget.id;
        wx.navigateTo({
            url: '../operate/remote/remote?id='+id+'&no='+no
        })
    },
    blueControl:function(event){
        var equipment_id = event.currentTarget.dataset.equipment_id;
        var id = event.currentTarget.id;
        wx.navigateTo({
            url: '../operate/blueControl/blueControl?id='+id+'&equipment_id='+equipment_id
        })
    },
    checkTime:function (time){
        var d = new Date(time);
        var _year = d.getFullYear();
        var _month = d.getMonth()+1;
        var _date = d.getDate();
        var _hour = d.getHours();
        var _minute = d.getMinutes();
        var _second = d.getSeconds();
        return _year+'-'+_month+'-'+_date+' '+_hour+':'+_minute+':'+_second;
    }
})

